﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementTypeManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.lblStrWhere.Text = string.Empty;
                this.BindData();
            }
        }
        private void BindData()
        {
           //// UART.BLL.Sales.SettlementType bll = new UART.BLL.Sales.SettlementType();
            var  ds = mk.l.Sales_SettlementType.ToList();
           this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
         }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                this.lblMsg.Text = string.Empty;
                this.lblMsg.Visible = false;
            //    UART.BLL.Sales.SettlementType bll = new UART.BLL.Sales.SettlementType();
                Sales_SettlementType m = new Sales_SettlementType();
                m.Flag = this.rbtnlFlag.SelectedValue;
                m.Name = mk.InputText(this.txtName.Text, 50);
                try
                {
                  //  bll.Add(m);
                    mk.l.Sales_SettlementType.InsertOnSubmit(m);
                    mk.l.SubmitChanges();
                    this.lblMsg.Text = "添加成功!";
                    this.lblMsg.Visible = true;
                    this.txtName.Text = string.Empty;
                    this.rbtnlFlag.ClearSelection();
                    this.BindData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "添加失败！错误信息：" + ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        /// <summary>
        /// Handles the RowEditing event of the myGridView control.设置编辑状态
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.myGridView.EditIndex = e.NewEditIndex;
            this.BindData();
        }
        /// <summary>
        /// Handles the RowCancelingEdit event of the myGridView control.取消编辑状态
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.myGridView.EditIndex = -1;
            this.BindData();
        }
        /// <summary>
        /// Handles the RowDeleting event of the myGridView control.删除数据
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewDeleteEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int intTypeID = int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["TypeID"].ToString());
            //不要删除前四项
            if (intTypeID > 9)
            {
                Sales_SettlementType bll = mk.l.Sales_SettlementType.Single(y => y.TypeID == intTypeID);
                try
                {
                  //  bll.Delete(intTypeID);
                    mk.l.Sales_SettlementType.DeleteOnSubmit(bll);
                    mk.l.SubmitChanges();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "删除出错！错误信息：" + ex.Message;
                }
            }
            //重新绑定数据
            this.BindData();
        }

        /// <summary>
        /// Handles the RowUpdating event of the myGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (this.IsValid)
            {
                this.lblMsg.Text = "";
                TextBox txtName = (TextBox)(this.myGridView.Rows[e.RowIndex].FindControl("txtName"));

               var bll = mk.l.Sales_SettlementType.ToList();
             Sales_SettlementType m = bll.Where(h=>h.TypeID==int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["TypeID"].ToString())).FirstOrDefault();
                m.Name = mk.InputText(txtName.Text, 50);
                try
                {
                  //  bll.Update(m);
                    mk.l.SubmitChanges();
                    this.myGridView.EditIndex = -1;
                    this.BindData();
                    //提交事务
                  //  ContextUtil.SetComplete();
                }
                catch (Exception ex)
                {
                    //撤销事务
                  //  ContextUtil.SetAbort();
                    this.lblMsg.Text = "修改失败！错误信息：" + ex.Message;
                }
            }
        }

        /// <summary>
        /// 绑定数据时显示状态信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int intTypeID = int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["TypeID"].ToString());
                //不要编辑前四项
                if (intTypeID < 10)
                {
                    LinkButton lbtnEdit = (LinkButton)e.Row.FindControl("lbtnEdit");
                    LinkButton lbtnDel = (LinkButton)e.Row.FindControl("lbtnDel");
                    lbtnDel.Visible = false;
                    lbtnDel.Enabled = false;
                    lbtnEdit.Visible = false;
                    lbtnEdit.Enabled = false;
                }
                string strFlag = this.myGridView.DataKeys[e.Row.RowIndex].Values["Flag"].ToString();
                Label lblFlag = (Label)(e.Row.FindControl("lblFlag"));
                switch (strFlag)
                {
                    case "1":
                        lblFlag.Text = "支出";
                        //lblFlag.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblFlag.Text = "收入";
                        //lblFlag.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblFlag.Text = "未知";
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

        /// <summary>
        /// Handles the ServerValidate event of the CustomValidator1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
          //  UART.BLL.Sales.SettlementType bll = new UART.BLL.Sales.SettlementType();
            if (mk.l.Sales_SettlementType.ToList().Exists(y=>y.Name==mk.InputText(this.txtName.Text, 50)&y.Flag==this.rbtnlFlag.SelectedValue))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        /// <summary>
        /// Handles the ServerValidate1 event of the CustomValidator1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
        {
           var bll =mk.l.Sales_SettlementType.ToList();
            CustomValidator val = (CustomValidator)source;
            //这里要引用父级控件，val的父级是单元格，再父级才是数据行
            GridViewRow gvr = (GridViewRow)(val.Parent.Parent);
            Sales_SettlementType m = bll.Where(h=>h.TypeID==int.Parse(gvr.Cells[0].Text)).FirstOrDefault();
            TextBox txtName = (TextBox)(val.Parent.FindControl("txtName"));
            if (txtName != null)
            {
                string strName = mk.InputText(txtName.Text, 50);
                int intTypeID = int.Parse(this.myGridView.DataKeys[gvr.RowIndex].Values["TypeID"].ToString());
                //要先判断一下，如果更改了产品名称的话，判断是否存在重复数据
                if (m.Name != strName)
                {
                    if (bll.Exists(y=>y.Name==strName&y.Flag==m.Flag))
                    {
                        args.IsValid = false;
                    }
                    else
                    {
                        args.IsValid = true;
                    }
                }
                else
                {
                    args.IsValid = true;
                }
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}
